package com.ymatou.shop.api;

import java.util.List;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.ymatou.shop.model.Address;
import com.ymatou.shop.model.Comment;
import com.ymatou.shop.model.LeaveMessage;
import com.ymatou.shop.model.LoginResult;
import com.ymatou.shop.model.Message;
import com.ymatou.shop.model.Order;
import com.ymatou.shop.model.PayBank;
import com.ymatou.shop.model.Product;
import com.ymatou.shop.model.ResponseMessage;
import com.ymatou.shop.model.Seller;
import com.ymatou.shop.util.GlobalUtil;
import com.ymatou.shop.util.JsonUtil;

public final class ApiResponse {

	private ApiResponse() {
	}

	/**
	 * 登录
	 * 
	 * @param jsonResult
	 * @return
	 */
	public static LoginResult getLoginResult(String jsonResult) {
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(jsonResult).getAsJsonObject().getAsJsonObject("Result");

			return JsonUtil.fromJson(jsonObject, LoginResult.class);
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return null;
		}
	}

	/**
	 * 获取账户信息
	 * 
	 * @param jsonResult
	 * @return
	 */
	public static LoginResult getLoginResultForAccountInf(String jsonResult) {
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(jsonResult).getAsJsonObject().getAsJsonObject("Result");

			return JsonUtil.fromJson(jsonObject, LoginResult.class);
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return null;
		}
	}

	public static Seller getSeller(String jsonResult) {
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(jsonResult).getAsJsonObject().getAsJsonObject("Result");

			return JsonUtil.fromJson(jsonObject, Seller.class);
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return null;
		}
	}

	/**
	 * 获取用户信息
	 * 
	 * @param jsonResult
	 * @return
	 */
	public static LoginResult getAccountInfo(String jsonResult) {
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(jsonResult).getAsJsonObject().getAsJsonObject("Result");

			return JsonUtil.fromJson(jsonObject, LoginResult.class);
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return null;
		}
	}
	
	public static List<PayBank> getPayBankList(String response) {
		try {
			JsonParser parser = new JsonParser();
			JsonArray jsonArray = parser.parse(response).getAsJsonObject().getAsJsonArray("Result");
			
			return JsonUtil.fromJsonToList(jsonArray, PayBank.class);
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return null;
		}
	}

	/**
	 * 获取首页列表
	 * 
	 * @param jsonResult
	 * @return
	 */
	public static List<Product> getHomeList(String jsonResult) {
		try {
			JsonParser parser = new JsonParser();
			JsonArray jsonArray = parser.parse(jsonResult).getAsJsonObject().getAsJsonArray("Result");

			return JsonUtil.fromJsonToList(jsonArray, Product.class);
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return null;
		}
	}

	public static List<Product> getSellerProductList(String jsonResult) {
		try {
			JsonParser parser = new JsonParser();
			JsonArray jsonArray = parser.parse(jsonResult).getAsJsonObject().getAsJsonArray("Result");

			return JsonUtil.fromJsonToList(jsonArray, Product.class);
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return null;
		}
	}

	public static Product getSellerProduct(String jsonResult) {
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(jsonResult).getAsJsonObject().getAsJsonObject("Result");

			return JsonUtil.fromJson(jsonObject, Product.class);
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return null;
		}
	}

	/**
	 * 获取用户收货地址列表
	 * 
	 * @param jsonResult
	 * @return
	 */
	public static List<Address> getUserAddressList(String jsonResult) {
		try {
			JsonParser parser = new JsonParser();
			JsonArray jsonArray = parser.parse(jsonResult).getAsJsonObject().getAsJsonArray("Result");

			return JsonUtil.fromJsonToList(jsonArray, Address.class);
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return null;
		}
	}

	public static int getSellerNewProductNum(String jsonResult) {
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(jsonResult).getAsJsonObject().getAsJsonObject("Result");

			return jsonObject.get("ProductNum").getAsInt();
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return 0;
		}
	}

	/**
	 * 获取消息数
	 * 
	 * @param jsonResult
	 * @return
	 */
	public static int getNewCommentNum(String jsonResult) {
		GlobalUtil.logE("get new comment num");
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(jsonResult).getAsJsonObject();

			return jsonObject.get("Result").getAsInt();
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return 0;
		}
	}

	/**
	 * 
	 * @param jsonResult
	 * @return
	 */
	public static int getNewOrderNum(String jsonResult) {
		GlobalUtil.logE("get new order num");
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(jsonResult).getAsJsonObject();

			return jsonObject.get("Result").getAsInt();
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return 0;
		}
	}

	/**
	 * 验证买家邀请码
	 * 
	 * @param response
	 * @return
	 */
	public static ResponseMessage invitationCode(String response) {
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(response).getAsJsonObject();
			return JsonUtil.fromJson(jsonObject, ResponseMessage.class);
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
		}
		return null;
	}

	/**
	 * 添加商品
	 * 
	 * @param response
	 * @return
	 */
	public static ResponseMessage addProduct(String response) {
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(response).getAsJsonObject();
			return JsonUtil.fromJson(jsonObject, ResponseMessage.class);
		} catch (Throwable e) {
			e.printStackTrace();
			return new ResponseMessage();
		}
	}

	/**
	 * 发评论
	 * 
	 * @param response
	 * @return
	 */
	public static ResponseMessage sendComment(String response) {
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(response).getAsJsonObject();
			return JsonUtil.fromJson(jsonObject, ResponseMessage.class);
		} catch (Throwable e) {
			e.printStackTrace();
			return new ResponseMessage();
		}
	}

	/**
	 * 回复评论
	 * 
	 * @param response
	 * @return
	 */
	public static boolean sendCommentReply(String response) {
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(response).getAsJsonObject();
			return jsonObject.get("Status").getAsInt() == 200;
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return false;
		}
	}

	/**
	 * 获取权限
	 * 
	 * @param response
	 * @return
	 */
	public static ResponseMessage permission(String response) {
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(response).getAsJsonObject();
			return JsonUtil.fromJson(jsonObject, ResponseMessage.class);
			//			JsonObject jsonObject = parser.parse(response).getAsJsonObject().getAsJsonObject("Result");

		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
		}
		return null;
	}

	public static ResponseMessage canclePermission(String response) {
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(response).getAsJsonObject();
			return JsonUtil.fromJson(jsonObject, ResponseMessage.class);
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
		}
		return null;
	}

	/**
	 * 增加，删除用户收货地址
	 * 
	 * @param response
	 * @return
	 */
	public static ResponseMessage addOrDeleteAddress(String response) {
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(response).getAsJsonObject();
			return JsonUtil.fromJson(jsonObject, ResponseMessage.class);
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
		}
		return null;
	}
	
	/**
	 * 下单
	 * @param response
	 * @return
	 */
	public static ResponseMessage saveOrder(String response) {
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(response).getAsJsonObject();
			ResponseMessage responseMessage = JsonUtil.fromJson(jsonObject, ResponseMessage.class);
			JsonObject mJsonObject = jsonObject.getAsJsonObject("Result");
			responseMessage.payUrl = mJsonObject.get("PayUrl").getAsString();

			return responseMessage;
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
		}
		return null;
	}

	public static List<Product> getProductList(String jsonResult) {
		try {
			JsonParser parser = new JsonParser();
			JsonArray jsonArray = parser.parse(jsonResult).getAsJsonObject().getAsJsonArray("Result");
			return JsonUtil.fromJsonToList(jsonArray, Product.class);
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return null;
		}
	}

	/**
	 * 获取评论列表
	 * 
	 * @param jsonResult
	 * @return
	 */
	public static List<Comment> getCommentList(String jsonResult) {
		try {
			JsonParser parser = new JsonParser();
			JsonArray jsonArray = parser.parse(jsonResult).getAsJsonObject().getAsJsonArray("Result");

			return JsonUtil.fromJsonToList(jsonArray, Comment.class);
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return null;
		}
	}

	public static List<LeaveMessage> getProductLeaveMessageList(String jsonResult) {
		try {
			JsonParser parser = new JsonParser();
			JsonArray jsonArray = parser.parse(jsonResult).getAsJsonObject().getAsJsonArray("Result");

			return JsonUtil.fromJsonToList(jsonArray, LeaveMessage.class);
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return null;
		}
	}

	public static ResponseMessage getUploadImageUrl(String jsonResult) {
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(jsonResult).getAsJsonObject();
			ResponseMessage responseMessage = JsonUtil.fromJson(jsonObject, ResponseMessage.class);
			JsonObject mJsonObject = jsonObject.getAsJsonObject("Result");
			responseMessage.picUrl = mJsonObject.get("PicUrl").getAsString();
//			try {
//				StringMap<String> mStringMap = (StringMap<String>) responseMessage.result;
//				responseMessage.picUrl = mStringMap.get("PicUrl");
//			} catch (Exception e) {
//				// TODO: handle exception
//			}
			return responseMessage;
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return null;
		}
	}
	
	public static ResponseMessage getAddressId(String jsonResult) {
		try {
			JsonParser parser = new JsonParser();
			JsonObject jsonObject = parser.parse(jsonResult).getAsJsonObject();
			ResponseMessage responseMessage = JsonUtil.fromJson(jsonObject, ResponseMessage.class);
			JsonObject mJsonObject = jsonObject.getAsJsonObject("Result");
			responseMessage.addressId = mJsonObject.get("AddressId").getAsString();
			return responseMessage;
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return null;
		}
	}

	public static List<Order> getOrderList(String jsonResult) {
		try {
			JsonParser parser = new JsonParser();
			JsonArray jsonArray = parser.parse(jsonResult).getAsJsonObject().getAsJsonArray("Result");

			return JsonUtil.fromJsonToList(jsonArray, Order.class);
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return null;
		}
	}
	
	public static List<Message> getMessageList(String jsonResult) {
		try {
			JsonParser parser = new JsonParser();
			JsonArray jsonArray = parser.parse(jsonResult).getAsJsonObject().getAsJsonArray("Result");

			return JsonUtil.fromJsonToList(jsonArray, Message.class);
		} catch (Throwable e) {
			GlobalUtil.logE(e.getMessage(), e);
			return null;
		}
	}
}
